// 处理切片
export const handleFile = (file: File, chunkSize: number) => {
  let chunks = [];
  const totalChunks = Math.ceil(file.size / chunkSize);
  for (let i = 0; i < totalChunks; i++) {
    let start = i * chunkSize;
    let end = Math.min(file.size, start + chunkSize);
    let blob = file.slice(start, end);
    chunks.push(blob);
  }
  return { chunks, totalChunks };
};

// 切片上车

export const sliceUpload = async (
  filename: string,
  type: string,
  uuid: string,
  chunkTotal: number,
  chunks: Blob[] | File[],
  pid: number,
  size: number,
  api: (Form: FormData, query: any) => Promise<any>,
  list: any
) => {
  for (let i = 0; i < chunkTotal; i++) {
    let Form = new FormData();
    Form.append("filename", filename);
    Form.append("uuid", uuid);
    Form.append("index", `${i}`);
    Form.append("file", chunks[i]);
    Form.append("type", type);
    Form.append("chunkTotal", `${chunkTotal}`);
    Form.append("size", `${size}`);
    const { data } = (await api!(Form, pid)) as any;

    // 找出文件
    const index = list.findIndex((e: any) => e.uuid === data.uuid);
    if (index !== -1) {
      list[index].nowIndex = data.index;
      if (data.status === 1) {
        list[index].status = 1;
      }
    }
  }
};
